<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教研组" prop="teacherDeptId">
        <treeselect style="width:200px" v-model="teacherDeptId" :options="campusOptions" 
                :multiple="false" :disableBranchNodes="true"
                :show-count="true" placeholder="请选择教师教研组" />
      </el-form-item>
      <el-form-item label="被评教师" prop="teachUserId">
        <el-select
              v-model="queryParams.teachUserId"
              placeholder="请输入被评教师"
              clearable
              size="small"
              style="width: 200px"
            >
              <el-option
                v-for="user in teacherUserOptions"
                :key="user.userId"
                :label="user.nickName"
                :value="user.userId"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="被评科目" prop="subjectId">
        <el-select v-model="queryParams.subjectId" placeholder="请选择科目">
            <el-option
                v-for="subject in subjectOptions"
                :key="subject.subjectId"
                :label="subject.subjectName"
                :value="subject.subjectId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="被评班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择被评班级">
            <el-option
                v-for="classInfo in classInfoOptions"
                :key="classInfo.classId"
                :label="classInfo.className"
                :value="classInfo.classId"
              />
          </el-select>
      </el-form-item>
      <!--
      <el-form-item label="评分" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:appraiseRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:appraiseRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:appraiseRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:appraiseRecord:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
-->
    <el-table v-loading="loading" :data="appraiseRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="考试结果ID" align="center" prop="recordId" />-->
      <el-table-column label="评测试卷" align="center" prop="examName" />
      <el-table-column label="被评教师" align="center" prop="teachUserName">
        <template slot-scope="scope">
          <router-link :to="'/stuAppraise/stuAppraiseOptions/index/' + scope.row.recordId" class="link-type">
            <span>{{ scope.row.teachUserName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="被评科目" align="center" prop="subjectName" />
      <el-table-column label="被评班级" width="200px" align="center" prop="className" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
      <el-table-column label="操作" min-width="200px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:appraiseRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['edu:appraiseRecord:query']"
          ><router-link :to="'/stuAppraise/stuAppraiseOptions/index/' + scope.row.recordId" class="link-type">
            <span>学生评分</span>
          </router-link></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:appraiseRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学评价考试结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--
        <el-form-item label="考试ID" prop="examId">
          <el-input disabled v-model="form.examId" placeholder="请输入考试ID" />
        </el-form-item>
        <el-form-item label="被评教师ID" prop="teachUserId">
          <el-input v-model="form.teachUserId" placeholder="请输入被评教师ID" />
        </el-form-item>
        <el-form-item label="被评科目ID" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入被评科目ID" />
        </el-form-item>
        <el-form-item label="被评班级ID" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入被评班级ID" />
        </el-form-item>
        -->
        <el-form-item label="评分" prop="score">
          <el-input v-model="form.score" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
                <el-radio
                                  v-for="dict in statusOptions"
                                  :key="dict.dictValue"
                                  :label="dict.dictValue"
                                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppraiseRecord, getAppraiseRecord, delAppraiseRecord, addAppraiseRecord, updateAppraiseRecord, exportAppraiseRecord } from "@/api/edu/appraiseRecord";
import { ajaxList } from "@/api/edu/classInfo";
import { treeselectByType } from "@/api/system/dept";
import { getSubjectsAll } from "@/api/edu/subjects";
import { listAllUserByDeptId } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AppraiseRecord",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学评价考试结果表格数据
      appraiseRecordList: [],
      campusOptions: [],
      subjectOptions: [],
      teacherUserOptions: [],
      classInfoOptions: [],
      statusOptions: [],
      teacherDeptId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examId: null,
        teachUserId: null,
        subjectId: null,
        classId: null,
        score: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examId: [
          { required: true, message: "考试ID不能为空", trigger: "blur" }
        ],
        teachUserId: [
          { required: true, message: "被评教师ID不能为空", trigger: "blur" }
        ],
        subjectId: [
          { required: true, message: "被评科目ID不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "被评班级ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.queryCampus()
    this.getSubjects();
    this.getClassInfoAll()
    
    this.getList();
  },
  watch: {
    'teacherDeptId': 'selectQueryTeacher',
  },
  methods: {
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    queryCampus() {
      // 查询教学组（deptType = 4）
      treeselectByType("4").then(response => {
        this.campusOptions = response.data;
      })
    },
    getSubjects() {
      getSubjectsAll().then(response => {
        this.subjectOptions = response.data
      })
    },
    selectQueryTeacher() {
      if(this.teacherDeptId === undefined || this.teacherDeptId === null) {
        this.$message({
            type: 'warning',
            message: '请选择教学组'
          })
          return
      }
      listAllUserByDeptId(this.teacherDeptId).then(response => {
        this.teacherUserOptions = response.data
      })
    },
    getClassInfoAll() {
      ajaxList().then(response => {
        this.classInfoOptions = response.data
      })
    },
    /** 查询教学评价考试结果列表 */
    getList() {
      this.loading = true;
      listAppraiseRecord(this.queryParams).then(response => {
        this.appraiseRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        examId: null,
        teachUserId: null,
        subjectId: null,
        classId: null,
        examName: null,
        teachUserName: null,
        subjectName: null,
        className: null,
        score: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学评价考试结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getAppraiseRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教学评价考试结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateAppraiseRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppraiseRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$confirm('是否确认删除教学评价考试结果编号为"' + recordIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppraiseRecord(recordIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学评价考试结果数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppraiseRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
